package com.example8.multi_datasource_mybatisplus.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example8.multi_datasource_mybatisplus.entity.source.User;
import com.example8.multi_datasource_mybatisplus.entity.target.CustomerDO;
import com.example8.multi_datasource_mybatisplus.mapper.source.UserMapper;
import com.example8.multi_datasource_mybatisplus.mapper.target.CustomerMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @Description 测试服务
 * @Author lihw
 * @Date 2024/3/26 14:35
 */
@Slf4j
@Service
public class TestService {

    final CustomerMapper customerMapper;
    final UserMapper userMapper;
    private final PlatformTransactionManager primaryTransactionManager;
    private final PlatformTransactionManager secondaryTransactionManager;

    public TestService(CustomerMapper customerMapper, UserMapper userMapper, PlatformTransactionManager primaryTransactionManager, PlatformTransactionManager secondaryTransactionManager) {
        this.customerMapper = customerMapper;
        this.userMapper = userMapper;
        this.primaryTransactionManager = primaryTransactionManager;
        this.secondaryTransactionManager = secondaryTransactionManager;
    }

    @PostConstruct
    public void testTarget() {
        List<User> userList = userMapper.selectList(null);
        log.info("user: {}", userList);

        CustomerDO customerDO = customerMapper.selectOne(new QueryWrapper<CustomerDO>()
                .last("limit 1"));
        log.info("customerDO: {}", customerDO);
    }


    public void datasourceTest() {
        List<CustomerDO> customerDOS = customerMapper.queryList();
        log.info("target数据源测试数据: {}", customerDOS);

        List<User> userList = userMapper.getUserList();
        log.info("source数据源测试数据: {}", userList);
    }

    public void transactionalTest() {
        // 创建第一个数据源的事务定义
        DefaultTransactionDefinition def = new DefaultTransactionDefinition();
        // 事务1
        TransactionStatus transactionOne = primaryTransactionManager.getTransaction(def);
        // 事务2
        TransactionStatus transactionTwo = secondaryTransactionManager.getTransaction(def);
        // 事务标记 1 成功 0 失败
        Integer statusOne = 1;
        Integer statusTwo = 1;
        try {
            // 执行事务1 的逻辑
        } catch (Exception e) {
            statusOne = 0;
            e.printStackTrace();
        }

        try {
            // 执行事务2 的逻辑
        } catch (Exception e) {
            statusTwo = 0;
            e.printStackTrace();
        }

        // 提交
        if (statusOne == 1 && statusTwo == 1) {
            primaryTransactionManager.commit(transactionOne);
            secondaryTransactionManager.commit(transactionTwo);
        } else {
            // 回滚
            primaryTransactionManager.rollback(transactionOne);
            primaryTransactionManager.rollback(transactionTwo);
        }


    }

}
